package com.saxon.blog.controller;


import com.saxon.blog.pojo.BlogType;
import com.saxon.blog.service.Blog.BlogService;
import com.saxon.blog.service.Blog.BlogTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BlogTypeController {
    private BlogTypeService blogTypeService;
    private BlogService blogService;

    @Autowired
    public void setBlogService (BlogService blogService) {
        this.blogService = blogService;
    }

    @Autowired
    public void setBlogTypeService (BlogTypeService blogTypeService) {
        this.blogTypeService = blogTypeService;
    }

    @GetMapping ("/blogType/getAllBlogType")
    @ResponseBody
    public List<BlogType> getAllBlogType () {
        List<BlogType> blogTypes = blogTypeService.QueryAllType ();
        return blogTypes;
    }

    @GetMapping ("/blogType/getBlogTypeById")
    @ResponseBody
    public BlogType getBlogTypeByid (int type_id) {
        return blogTypeService.QueryBlogTypeById (type_id);
    }

    @GetMapping ("/admin/getAllBlogType")
    @ResponseBody
    public List<BlogType> getAllBlogTypeAdmin () {
        return blogTypeService.QueryAllType ();
    }

    @ApiOperation ("获得博客的种类和对应的博客数量")
    @GetMapping ("/admin/getBlogType")
    public String getBlogType (Model model) {
        List<BlogType> blogTypes = blogTypeService.QueryAllType ();
        for (int i = 0; i < blogTypes.size (); i++) {
            Map<String, Object> map = new HashMap<> ();
            map.put ("type_id", blogTypes.get (i).getId ());
            blogTypes.get (i).setNum (blogService.QueryBlogNum (map));
        }
        model.addAttribute ("blogTypes", blogTypes);
        return "admin/page/blogType";
    }

    @ApiOperation ("添加一个博客种类")
    @ResponseBody
    @PostMapping ("/admin/addBlogTypeByName/")
    public boolean addBlogTypeByName (String typeName) {
        return blogTypeService.insertBlogType (typeName) > 0;
    }

    @ApiOperation ("删除一个博客类别")
    @ResponseBody
    @DeleteMapping ("/admin/deleteBlogTypeById/")
    public boolean deleteBlogTypeById (int id) {
        //先修改你要删除的类别所关联的博客，设置type_id=null
        if (id == 1) {
            return false;
        }
        int type_id = id;
        blogService.updateBlogTypeID (type_id);
        return blogTypeService.deleteBlogType (id) > 0;
    }

    @ApiOperation ("修改一个博客的名字")
    @ResponseBody
    @PostMapping ("/admin/alterBlogTypeById/")
    public boolean alterBlogTypeById(String typeName,int id){
        return blogTypeService.updateBlogType (typeName,id)>0;
    }
}
